import moment from 'moment'
import * as Yup from 'yup'

// guardian options list
export const guardianStateOptions = [
  { label: 'Legal Guardian', value: '1' },
  { label: 'Parent', value: '2' },
  { label: 'Family Member', value: '3' },
  { label: 'Other', value: '4' }
]
export const gender = [
  {
    text: 'Other',
    value: 1
  },
  {
    text: 'Male',
    value: 2
  },
  {
    text: 'Female',
    value: 3
  },
  {
    text: 'NonBinary',
    value: 4
  },
  {
    text: 'Transgender',
    value: 5
  }
]
export const genderObj = {
  1: {
    text: 'Unknown',
    value: 1
  },
  2: {
    text: 'Male',
    value: 2
  },
  3: {
    text: 'Female',
    value: 3
  },
  4: {
    text: 'NonBinary',
    value: 4
  },
  5: {
    text: 'Transgender',
    value: 5
  }
}

export const maritalStatus = [
  {
    text: 'Married',
    value: 1
  },
  {
    text: 'Single',
    value: 2
  },
  {
    text: 'Divorced',
    value: 3
  },
  {
    text: 'Other',
    value: 4
  }
]
export const maritalStatusObj = {
  1: {
    text: 'Married',
    value: 1
  },
  2: {
    text: 'Single',
    value: 2
  },
  3: {
    text: 'Divorced',
    value: 3
  },
  4: {
    text: 'Other',
    value: 4
  }
}

export const Employment = [
  {
    text: 'Full time',
    value: 1
  },
  {
    text: 'Part time',
    value: 2
  },
  {
    text: 'Unemployed',
    value: 3
  },
  {
    text: 'NA',
    value: 4
  }
]
export const EmploymentObj = {
  1: {
    text: 'Full time',
    value: 1
  },
  2: {
    text: 'Part time',
    value: 2
  },
  3: {
    text: 'UnEmployed',
    value: 3
  },
  4: {
    text: 'NA',
    value: 4
  }
}

// email Reminder options
export const emailReminderOptions = [
  { label: 'Appointment', value: 'appointment' },
  { label: 'Billing', value: 'billing' }
]
// Phone options
export const phoneOptions = [
  {
    label: 'Yes, Its okay to send voice message to this number',
    value: 'voiceMessage'
  },
  {
    label: 'Yes, Its okay to send text message to this number',
    value: 'textMessage'
  }
]

// Race and Ethnicity options
export const raceAndEthnicityOptions = [
  {
    label: 'American Indian or Alaska Native',
    value: 1
  },
  { label: 'Asian', value: 5 },
  { label: 'Black or African American', value: 2 },
  { label: 'Hispanic or Latin', value: 6 },
  {
    label: 'Middle Eastern or North African',
    value: 3
  },
  {
    label: 'Native Hawaiian or other Pacific Islander',
    value: 7
  },
  { label: 'White', value: 4 },
  {
    label: 'Race or Ethnicity not listed',
    value: 8
  }
]

export const raceAndEthnicityOptionsLabel = {
  1: {
    label: 'American Indian or Alaska Native',
    value: 1
  },
  5: { label: 'Asian', value: 5 },
  2: { label: 'Black or African American', value: 2 },
  6: { label: 'Hispanic or Latin', value: 6 },
  3: {
    label: 'Middle Eastern or North African',
    value: 3
  },
  7: {
    label: 'Native Hawaiian or other Pacific Islander',
    value: 7
  },
  4: { label: 'White', value: 4 },
  8: {
    label: 'Race or Ethnicity not listed',
    value: 8
  }
}

export const languageList = [
  {
    text: 'Chinese',
    value: 'zh'
  },
  {
    text: 'English',
    value: 'en'
  },
  {
    text: 'Japanese',
    value: 'ja'
  },
  {
    text: 'Korean',
    value: 'ko'
  },
  {
    text: 'Hindi',
    value: 'hi'
  },
  {
    text: 'Spanish',
    value: 'es'
  },
  {
    text: 'Arabic',
    value: 'ar'
  },
  {
    text: 'Malay',
    value: 'ms'
  },
  {
    text: 'Russian',
    value: 'ru'
  },
  {
    text: 'Bengali',
    value: 'bn'
  },
  {
    text: 'PORTUGUESE',
    value: 'pt'
  },
  {
    text: 'FRENCH',
    value: 'fr'
  },
  {
    text: 'VIETNAMESE',
    value: 'vi'
  },
  {
    text: 'OTHER',
    value: 'ot'
  }
]
export const languageListObj = {
  zh: {
    text: 'Chinese',
    value: 'zh'
  },
  en: {
    text: 'English',
    value: 'en'
  },
  ja: {
    text: 'Japanese',
    value: 'ja'
  },
  ko: {
    text: 'Korean',
    value: 'ko'
  },
  hi: {
    text: 'Hindi',
    value: 'hi'
  },
  es: {
    text: 'Spanish',
    value: 'es'
  },
  ar: {
    text: 'Arabic',
    value: 'ar'
  },
  ms: {
    text: 'Malay',
    value: 'ms'
  },
  ru: {
    text: 'Russian',
    value: 'ru'
  },
  bn: {
    text: 'Bengali',
    value: 'bn'
  },
  pt: {
    text: 'PORTUGUESE',
    value: 'pt'
  },
  fr: {
    text: 'FRENCH',
    value: 'fr'
  },
  vi: {
    text: 'VIETNAMESE',
    value: 'vi'
  },
  ot: {
    text: 'OTHER',
    value: 'ot'
  }
}
// Billing Type options
export const billingTypeOptions = [
  { label: 'Self-Pay', value: '1' },
  { label: 'Insurance', value: '2' }
]

// Auto generated billing document options
export const autoGenerateBillingDocumentOptions = [
  {
    label: 'Automatically create monthly Statement for this client',
    value: 'monthlyStatement'
  },
  {
    label: 'Automatically create monthly Super bills for this client',
    value: 'monthlySuperBill'
  }
]

// Client services Options
export const clientServicesOptions = [
  { label: '90873', value: '89789', time: '60', charges: '150' },
  { label: '89789', value: '89789', time: '90', charges: '30' },
  { label: '12389', value: '12389', time: '120', charges: '45' },
  { label: '78689', value: '78689', time: '130', charges: '70' },
  { label: '89756', value: '89756', time: '180', charges: '800' }
]
export const State = [
  { text: 'AL', value: 'AL' },
  { text: 'AK', value: 'AK' },
  { text: 'AZ', value: 'AZ' },
  { text: 'AR', value: 'AR' },
  { text: 'CA', value: 'CA' },
  { text: 'CO', value: 'CO' },
  { text: 'CT', value: 'CT' },
  { text: 'DE', value: 'DE' },
  { text: 'FL', value: 'FL' },
  { text: 'GA', value: 'GA' },
  { text: 'HI', value: 'HI' },
  { text: 'ID', value: 'ID' },
  { text: 'IL', value: 'IL' },
  { text: 'IN', value: 'IN' },
  { text: 'IA', value: 'IA' },
  { text: 'KS', value: 'KS' },
  { text: 'KY', value: 'KY' },
  { text: 'LA', value: 'LA' },
  { text: 'ME', value: 'ME' },
  { text: 'MD', value: 'MD' },
  { text: 'MA', value: 'MA' },
  { text: 'MI', value: 'MI' },
  { text: 'MN', value: 'MN' },
  { text: 'MS', value: 'MS' },
  { text: 'MO', value: 'MO' },
  { text: 'MT', value: 'MT' },
  { text: 'NE', value: 'NE' },
  { text: 'NV', value: 'NV' },
  { text: 'NH', value: 'NH' },
  { text: 'NJ', value: 'NJ' },
  { text: 'NM', value: 'NM' },
  { text: 'NY', value: 'NY' },
  { text: 'NC', value: 'NC' },
  { text: 'ND', value: 'ND' },
  { text: 'OH', value: 'OH' },
  { text: 'OK', value: 'OK' },
  { text: 'OR', value: 'OR' },
  { text: 'PA', value: 'PA' },
  { text: 'RI', value: 'RI' },
  { text: 'SC', value: 'SC' },
  { text: 'SD', value: 'SD' },
  { text: 'TN', value: 'TN' },
  { text: 'TX', value: 'TX' },
  { text: 'UT', value: 'UT' },
  { text: 'VT', value: 'VT' },
  { text: 'VA', value: 'VA' },
  { text: 'WA', value: 'WA' },
  { text: 'WV', value: 'WV' },
  { text: 'WI', value: 'WI' },
  { text: 'WY', value: 'WY' }
]
const daysInMonth = (month, year) => {
  // List of days in each month (considering leap years for February)
  const daysByMonth = {
    1: 31,
    2: year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0) ? 29 : 28,
    3: 31,
    4: 30,
    5: 31,
    6: 30,
    7: 31,
    8: 31,
    9: 30,
    10: 31,
    11: 30,
    12: 31
  }
  return daysByMonth[month.toString()]
}
// // validation schema
// export const ClientFormValidationSchema = Yup.object().shape({
//   first_name: Yup.string()
//     .min(3, 'First name should be at least 3 characters long')
//     .max(15, 'First name should not exceed more than 15 characters')
//     .required('First Name is a required field'),
//   middle_name: Yup.string()
//     .min(3, 'Middle name should be at least 3 characters long')
//     .max(10, 'Middle name should not exceed more than 10 characters'),
//   last_name: Yup.string()
//     .min(3, 'Last name should be at least 3 characters long')
//     .max(15, 'Last name should not exceed more than 15 characters')
//     .required('Last Name is a required field'),
//   suffix: Yup.string()
//     .min(2, 'Suffix should be at least 2 characters long')
//     .max(5, 'Suffix should not exceed more than 15 characters'),
//   preferred_name: Yup.string()
//     .min(3, 'Preferred name should be at least 3 characters long')
//     .max(20, 'Preferred name should not exceed more than 20 characters'),
//   client_guardian: Yup.array().of(
//     Yup.object().shape({
//       status: Yup.string(),
//       first_name: Yup.string()
//         .min(3, 'First name should be at least 3 characters long')
//         .max(15, 'First name should not exceed more than 15 characters'),
//       last_name: Yup.string()
//         .min(3, 'Last name should be at least 3 characters long')
//         .max(15, 'Last name should not exceed more than 15 characters'),
//       phone_number: Yup.string().nullable(),
//       email: Yup.string().email(),
//       address: Yup.string()
//         .min(3, 'Street Address has to be at least 5 characters long')
//         .max(50, "Street Address shouldn't exceed more than 50 characters"),
//       city: Yup.string()
//         .min(2, 'City has to be at least 5 characters long')
//         .max(20, "City shouldn't exceed more than 20 characters"),
//       state: Yup.string().max(
//         2,
//         'Country code should not exceed more than 2 characters'
//       ),
//       zipcode: Yup.number()
//         .min(3, 'Zip code has to be at least 3 characters long ')
//         .max(7, 'Zip code should not exceed more than 7 characters')
//     })
//   ),
//   phone_number: Yup.string(),
//   email: Yup.string().email('Please enter a valid email address'),
//   emailReminder: Yup.array().of(Yup.string()).nullable(),
//   phoneChecks: Yup.array().of(Yup.string()).nullable(),
//   addresses: Yup.array().of(
//     Yup.object().shape({
//       address: Yup.string()
//         .min(3, 'Street Address has to be at least 5 characters long')
//         .max(50, "Street Address shouldn't exceed more than 50 characters"),
//       city: Yup.string()
//         .min(2, 'City has to be at least 5 characters long')
//         .max(20, "City shouldn't exceed more than 20 characters"),
//       state: Yup.string().max(
//         2,
//         'Country code should not exceed more than 2 characters'
//       ),
//       zipcode: Yup.string()
//         .min(3, 'Zip code has to be at least 3 characters long ')
//         .max(7, 'Zip code should not exceed more than 7 characters')
//     })
//   ),
//   date_of_birth: Yup.string(),
//   gender: Yup.string(),
//   gender_identity: Yup.string().max(
//     140,
//     'Gender Identity should not exceed more than 140 characters'
//   ),
//   relationship_status: Yup.string(),
//   employment_status: Yup.string(),
//   race: Yup.string(),
//   ethnicity: Yup.string().max(
//     140,
//     "Race & Ethnicity details shouldn't exceed more than 140 characters "
//   ),
//   preferred_language: Yup.string(),
//   note: Yup.string(),
//   date_started: Yup.string(),
//   referred_by: Yup.string(),
//   billing_type: Yup.string()
// })

// Client Profile validation schema
export const ClientFormValidationSchema = Yup.object().shape(
  {
    first_name: Yup.string()
      .transform((value) => (typeof value === 'string' ? value.trim() : value))
      .min(3, 'First name should be at least 3 characters long')
      .max(15, 'First name should not exceed more than 15 characters')
      .required('First Name is a required field'),
    middle_name: Yup.string()
      .min(3, 'Middle name should be at least 3 characters long')
      .max(10, 'Middle name should not exceed more than 10 characters'),
    last_name: Yup.string()
      .transform((value) => (typeof value === 'string' ? value.trim() : value))
      .min(3, 'Last name should be at least 3 characters long')
      .max(15, 'Last name should not exceed more than 15 characters')
      .required('Last Name is a required field'),
    suffix: Yup.string()
      .min(2, 'Suffix should be at least 2 characters long')
      .max(10, 'Suffix should not exceed more than 10 characters'),
    preferred_name: Yup.string()
      .min(3, 'Preferred name should be at least 3 characters long')
      .max(20, 'Preferred name should not exceed more than 20 characters'),
    is_minor: Yup.boolean(),
    client_guardian: Yup.array().when('is_minor', {
      is: true,
      then: () => {
        return Yup.array().of(
          Yup.object().shape({
            status: Yup.string(),
            first_name: Yup.string()
              .min(3, 'First name should be at least 3 characters long')
              .max(15, 'First name should not exceed more than 15 characters'),
            last_name: Yup.string()
              .min(3, 'Last name should be at least 3 characters long')
              .max(15, 'Last name should not exceed more than 15 characters'),
            phone_number: Yup.string()
              .nullable()
              .required('Phone number is a required field'),
            email: Yup.string()
              .email('Please enter a valid email address')
              .required('Email is a required field'),
            address: Yup.string()
              .min(3, 'Street Address has to be at least 5 characters long')
              .max(
                50,
                "Street Address shouldn't exceed more than 50 characters"
              ),
            city: Yup.string()
              .min(2, 'City has to be at least 5 characters long')
              .max(20, "City shouldn't exceed more than 20 characters"),
            state: Yup.string().max(
              2,
              'Country code should not exceed more than 2 characters'
            ),
            zipcode: Yup.string()
              .min(3, 'Zip code has to be at least 3 characters long ')
              .max(7, 'Zip code should not exceed more than 7 characters')
          })
        )
      }
    }),
    phone_number: Yup.string(),
    email: Yup.string().email().required('Email is a required field'),
    emailReminder: Yup.array().of(Yup.string()).nullable(),
    addresses: Yup.array().of(
      Yup.object().shape({
        address: Yup.string()
          .min(3, 'Street Address has to be at least 5 characters long')
          .max(50, "Street Address shouldn't exceed more than 50 characters"),
        city: Yup.string()
          .min(2, 'City has to be at least 5 characters long')
          .max(20, "City shouldn't exceed more than 20 characters"),
        state: Yup.string()
          .nullable()
          .max(2, 'Country code should not exceed more than 2 characters'),
        zipcode: Yup.string()
          .nullable()
          .min(3, 'Zip code has to be at least 3 characters long ')
          .max(7, 'Zip code should not exceed more than 7 characters')
      })
    ),
    day: Yup.string()
      .test('day', 'Enter valid day', (value, { parent }) => {
        const { month, year } = parent || {}
        const daysInSelectedMonth = daysInMonth(parseInt(month), parseInt(year))
        return (
          (value <= daysInSelectedMonth && value > 0) || value === undefined
        )
      })
      .when(['month', 'year'], {
        is: (month, year) => month?.length > 0 || year?.length > 0,
        then: (schema) => schema.required('Please fill')
      }),
    month: Yup.string()
      .test('month', 'Enter valid month', (value) => {
        return (value <= 12 && value > 0) || value === undefined
      })
      .when(['day', 'year'], {
        is: (day, year) => day?.length > 0 || year?.length > 0,
        then: (schema) => schema.required('Please fill')
      }),
    year: Yup.string()
      .test('valid-year', 'Enter a valid year', (value) => {
        return (value && /^\d{4}$/.test(value)) || value === undefined
      })
      .test('future-year', 'Enter a valid year', (value) => {
        const currentYear = new Date().getFullYear()
        return (
          (value &&
            /^\d{4}$/.test(value) &&
            parseInt(value, 10) < currentYear) ||
          value === undefined
        )
      })
      .when(['month', 'day'], {
        is: (month, day) => month?.length > 0 || day?.length > 0,
        then: (schema) => schema.required('Please fill')
      }),
    date_of_birth: Yup.string(),
    gender: Yup.string(),
    gender_identity: Yup.string().max(
      140,
      'Gender Identity should not exceed more than 140 characters'
    ),
    relationship_status: Yup.string(),
    employment_status: Yup.string(),
    races: Yup.array(),
    ethnicity: Yup.string().max(
      140,
      "Race & Ethnicity details shouldn't exceed more than 140 characters "
    ),
    preferred_language: Yup.string(),
    note: Yup.string(),
    formatted_date_started: Yup.string()
      .matches(
        /^\d{2}\/\d{2}\/\d{4}$/,
        'Start Date must be in MM/DD/YYYY format'
      )
      .test('valid-date', 'Invalid date', function (value) {
        const format = moment.unix(value).format('MM/DD/YYYY')

        if (!value) {
          return true // Empty field is handled by the "required" validation
        }

        const parts =
          format === 'Invalid date' ? value.split('/') : format.split('/')
        const month = parseInt(parts[0], 10)
        const day = parseInt(parts[1], 10)
        const year = parseInt(parts[2], 10)

        if (month < 1 || month > 12 || day < 1 || day > 31 || year.length < 4) {
          return false
        }

        return true
      }),

    // date_started: Yup.string()
    //   // .matches(/^\d{2}\/\d{2}\/\d{4}$/, 'Start Date must be in MM/DD/YYYY format')
    //   .matches(
    //     /^(0[1-9]|1[0-2])\/(0[1-9]|[12][0-9]|3[01])\/\d{4}$/,
    //     'Date must be in the format mm/dd/yyyy'
    //   ),
    // .test('valid-date', 'Invalid date', function (value) {
    //   // const format = moment.unix(value).format('MM/DD/YYYY')
    //   if (!value) {
    //     return true // Empty field is handled by the "required" validation
    //   }

    //   // const parts =
    //   //   format === 'Invalid date' ? value.split('/') : format.split('/')

    //   // const month = parseInt(parts[0], 10)
    //   // const day = parseInt(parts[1], 10)
    //   // const year = parseInt(parts[2], 10)
    //   // if (month < 1 || month > 12 || day < 1 || day > 31 || year.length < 4) {
    //   //   return false
    //   // }

    //   return true
    // }),
    referred_by: Yup.string(),
    billing_type: Yup.string()
  },
  [
    ['day', 'month'],
    ['day', 'year'],
    ['month', 'year'],
    ['month', 'day'],
    ['year', 'month'],
    ['year', 'day']
  ]
)
